﻿using L.EntityFramework;
using MySql.Data.MySqlClient;
using NB.MGJ.Common.Utility;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service.Origins
{
    public class OriginsAuthService : BaseService
    {

        private BaseRepository _baseRepository = new BaseRepository();
        private BaseRepository<L.EntityModel.EntityContext> baseRepository2;
          public OriginsAuthService()
        {
            baseRepository2 = CreateBaseRepository<L.EntityModel.EntityContext>();
        }
        #region  增
        public int Add(L.EntityModel.Origins_Auth model)
        {
            int result = baseRepository2.Insert<L.EntityModel.Origins_Auth>(model);
            return result;
        }
        #endregion

        #region 删
        public int Del(int id)
        {
            var sql = string.Format(" UPDATE Origins_Auth  SET ViewDel=1 WHERE Id = {0}", id);
            int result = _baseRepository.ExecuteSql<int>(sql);
            return result;
        }
        #endregion

        #region 改
        public int Update(L.EntityModel.Origins_Auth model)
        {
            int result = baseRepository2.Update<L.EntityModel.Origins_Auth>(model);
            return result;
        }
        #endregion

        #region 查
        public Origins_Auth GetModel(int id)
        {
            string sql = "select * from Origins_Auth where Id=" + id;
            var model = _baseRepository.ExecuteSqlCollection<Origins_Auth>(sql).ToList().FirstOrDefault();
            return model;
        }



        public PagedList<Origins_Auth> GetPageList(ref PagerModel pager,LogOnUserModel user)
        {

            try
            {
                //StringBuilder strSql1 = new StringBuilder();
                //strSql1.Append(@" Origins_Auth ");
                //var strSql = new StringBuilder(strSql1.ToString().Trim(','));

                pager.FldName = " * ";

                pager.TblName = @"(select a.*,b.EnterpriseName from origins_auth as a LEFT JOIN organization_enterprise as b on a.EnterpriseId=b.ID) z";

                if (string.IsNullOrEmpty(pager.StrCondition) || pager.StrCondition == " ")
                {
                    pager.StrCondition = " ViewDel=0 and EnterpriseId=" + user.EnterpriseId;
                }
                if (string.IsNullOrEmpty(pager.FldSort) || pager.FldSort == "")
                    pager.FldSort = "  ChangeDate desc ";

                MySqlParameter[] parameters = _baseRepository.SqParameterRefactor(ref pager);


                IEnumerable<Origins_Auth> resultList = _baseRepository.ExecuteStoredProcedureCollection<Origins_Auth>("Common_PageList", parameters);
                return new PagedList<Origins_Auth>(resultList.ToList(), pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Origins_Auth> GetAuthListByEID(int EID)
        {
            var sql = @"select * from Origins_Auth where EnterpriseId=" + EID;
            var result = _baseRepository.ExecuteSqlCollection<Origins_Auth>(sql).ToList();
            return result;
        }




        #endregion
    }
}
